package com.globallogic.dailyplanner.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.globallogic.dailyplanner.dto.User;
import com.globallogic.dailyplanner.model.UserDetailsImpl;
import com.globallogic.dailyplanner.service.UserService;

@Service
public class UserDetailsServiceImpl implements UserDetailsService{
	
	@Autowired
	private UserService userService;

	@Transactional(propagation=Propagation.REQUIRES_NEW)
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		UserDetails userDetails = null;
		
		User user = userService.findOneByName(username);
		if(user != null){
			userDetails = new UserDetailsImpl(user);
		}
		
		return userDetails;
	}

}
